package com.webshop.Dao;

import com.webshop.database.ConnPool;
import com.webshop.database.JDBCUtil;
import com.webshop.domain.Items;
import com.webshop.domain.user.User;

import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductDao {
    public static Items getItem(int id){
        Items items = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        String sql = "select * from electronic_product where id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,String.valueOf(id));
            rs = ps.executeQuery();
            while (rs.next()){
                items = new Items();
                items.setPclass(rs.getString("pclass"));
                items.setPinfo(rs.getString("pinfo"));
                items.setSinfo(rs.getString("sinfo"));
                items.setPname(rs.getString("pname"));
                items.setPimgsrc(rs.getString("pimgsrc"));
                items.setPprice(Integer.parseInt(rs.getString("pprice")));
                items.setId(Integer.parseInt(rs.getString("id")));
                items.setAttr(rs.getString("attr"));
                items.setAttrv(rs.getString("attrv"));
                items.setWhos(rs.getString("whos"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return items;
        }

    }
    private static List convertList(ResultSet rs) throws SQLException {
        List list = new ArrayList();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {
            Map rowData = new HashMap();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }

    public static void update(String key,String value,int id){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        String sql = "update electronic_product set  "+key+"=?  where id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,value);
            ps.setString(2,String.valueOf(id));
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }
    public  static void  delete(int id){
        deleteItem(id);
    }
    public static void deleteItem(int id){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        String sql = "delete from electronic_product where id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,String.valueOf(id));
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }
    public static void changeid(int id){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        String sql = "update electronic_product set id = id-1 where id>"+id+";";
        try {
            ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }
    public static int maxid (){

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        int max=0;
        String sql = "select max(id)+1 from electronic_product";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()){
                max = Integer.parseInt(rs.getString("max(id)+1"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return max;
        }
    }
    public static void update(){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int max = ProductDao.maxid();
        conn = ConnPool.getConn();
        String sql = "ALTER TABLE electronic_product AUTO_INCREMENT = "+max+";";
        try {
            ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }

    public static List<Items> getItems(User user){
        List<Items> items = new ArrayList<Items>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        String sql = "select * from electronic_product where whos=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            rs = ps.executeQuery();
            while (rs.next()){
                Items items1 = new Items();
                items1.setPname(rs.getString("pname"));
                items1.setPprice(Integer.parseInt(rs.getString("pprice")));
                items1.setPclass(rs.getString("pclass"));
                items1.setId(rs.getInt("id"));
                items.add(items1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtil.release(conn,ps,rs);
            return items;
        }
    }
    public static void add(User user){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        String sql = "INSERT INTO electronic_product value(null,0,null,NULL,NULL,NULL,NULL,null,null,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }
    public static void add(Items items) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        String sql = "INSERT INTO electronic_product value(?,?,?,?,?,null,?,?,?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, items.getPname());
            ps.setInt(2, items.getPprice());
            ps.setString(3, items.getPinfo());
            ps.setString(4, items.getPimgsrc());
            ps.setString(5, items.getPclass());
            ps.setString(6, items.getAttr());
            ps.setString(7, items.getAttrv());
            ps.setString(8, items.getSinfo());
            ps.setString(9, items.getWhos());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(conn, ps, rs);
        }
    }
    public static List<Items> search(String word){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Items> items = new ArrayList<Items>();
        conn = ConnPool.getConn();
        String sql = "select * from electronic_product where pname like ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,"%"+word+"%");
            rs = ps.executeQuery();
            while (rs.next()){
                Items items1 = new Items();
                items1.setPname(rs.getString("pname"));
                items1.setPprice(Integer.parseInt(rs.getString("pprice")));
                items1.setPinfo(rs.getString("pinfo"));
                items1.setPimgsrc(rs.getString("pimgsrc"));
                items1.setPclass(rs.getString("pclass"));
                items1.setId(rs.getInt("id"));
                items1.setAttr(rs.getString("attr"));
                items1.setAttrv(rs.getString("attrv"));
                items1.setSinfo(rs.getString("sinfo"));
                items1.setWhos(rs.getString("whos"));
                items.add(items1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtil.release(conn,ps,rs);
            return items;
        }
    }
//    public static void main(String[] args) {
//        List<Items> search = null;
//        search = search("南瓜");
//        for (Items i: search){
//            System.out.println(i);
//        }
//    }
}
